/*
 * meituan.com Inc.
 * Copyright (c) 2010-2018 All Rights Reserved.
 */
package jdk.bio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * 基于BIO的时间服务器，采用传统的一个请求一个线程的模式
 * </p>
 * @author LvJing
 * @version $Id:TimeServer.java v1.0 2018-11-25 11:33 LvJing Exp $
 */
public class TimeServer {

    public static void main(String[] args) {
        int port = 8080;
        ServerSocket server = null;
        try {
            server = new ServerSocket(port);
            System.out.println("TimeServer listen on " + port);

            while (true) {
                Socket socket = server.accept();
                new Thread(new TimeServerHandler(socket)).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (server != null) {
                try {
                    server.close();
                    System.out.println("TimeServer Socket close!");
                } catch (IOException e1) {
                    System.out.println("TimeServer Socket close failed, errorMsg:" + e1.getMessage());
                }
            }
        }
    }

    public static class TimeServerHandler implements Runnable {

        private Socket socket;

        public TimeServerHandler(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            BufferedReader in = null;
            PrintWriter out = null;
            try {
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                out = new PrintWriter(socket.getOutputStream(), true);

                String body = null;
                while (true) {
                    if ((body = in.readLine()) == null) {
                        break;
                    }

                    System.out.println("TimeServer receive msg:" + body);
                    String response = body.equalsIgnoreCase("QUERY CURRENT TIME") ?
                            LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) : "Error Command";
                    out.println(response);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e1) {
                        System.out.println("Socked InputStream close failed, errorMsg:{}" + e1.getMessage());
                    }
                }
                if (out != null) {
                    out.close();
                }
                if (socket != null) {
                    try {
                        socket.close();
                        System.out.println("Socket close!");
                    } catch (IOException e1) {
                        System.out.println("Socket close failed, errorMsg:" + e1.getMessage());
                    }
                }
            }
        }
    }
}
